/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewKnowledgeBaseFrame.java
 *
 * Created on 10/10/2009, 15:42:39
 */

package gui;

import base.KnowledgeBaseManager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import logicaDePredicados.LogicPredicate;
import logicaDePredicados.PredicateLogic;
import logicaDePredicados.SkolemFunction;

/**
 *
 * @author Francisco Gindre
 */
public class NewKnowledgeBaseFrame extends javax.swing.JFrame {
    private File selectedFile;
    
    List<PredicateLogic>elementList;
    


    /** Creates new form NewKnowledgeBaseFrame */
    public NewKnowledgeBaseFrame(File file) {
        selectedFile=file;
        this.setName("Modify Knowledge Base");
        this.setVisible(true);
        this.setLocationRelativeTo(this.getParent());
        try {
            elementList = KnowledgeBaseManager.ObtainEntireKnowledgeBase(selectedFile);
        } catch (Exception ex) {
            ErrorFrame error = new ErrorFrame("Database Error: "+ex.getMessage());
            dispose();
        }
        
        
        initComponents();

    }
    public NewKnowledgeBaseFrame (){
        initComponents();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        closeButton = new javax.swing.JButton();
        newPredicateButton = new javax.swing.JButton();
        newSkolemButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        predicateComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setText(selectedFile.getAbsolutePath());
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Base File:");
        jLabel1.setName("jLabel1"); // NOI18N

        closeButton.setText("Close");
        closeButton.setName("closeButton"); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        newPredicateButton.setText("New Predicate");
        newPredicateButton.setName("newPredicateButton"); // NOI18N
        newPredicateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newPredicateButtonActionPerformed(evt);
            }
        });

        newSkolemButton.setText("New Skolem Function");
        newSkolemButton.setName("newSkolemButton"); // NOI18N
        newSkolemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newSkolemButtonActionPerformed(evt);
            }
        });

        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/icons/background.png"))); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        editButton.setText("edit");
        editButton.setName("editButton"); // NOI18N
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("delete");
        deleteButton.setName("deleteButton"); // NOI18N
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        refreshButton.setText("Refresh");
        refreshButton.setName("refreshButton"); // NOI18N
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        predicateComboBox.setModel(new javax.swing.DefaultComboBoxModel(elementList.toArray()));
        predicateComboBox.setName("predicateComboBox"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                .addGap(111, 111, 111))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(558, Short.MAX_VALUE)
                .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(newPredicateButton, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                                .addGap(159, 159, 159)
                                .addComponent(predicateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(editButton, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2))
                            .addComponent(newSkolemButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(refreshButton, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(deleteButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(644, Short.MAX_VALUE)
                        .addComponent(jLabel3)))
                .addGap(35, 35, 35))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(refreshButton))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(editButton)
                        .addComponent(deleteButton)
                        .addComponent(predicateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(newPredicateButton))
                .addGap(30, 30, 30)
                .addComponent(newSkolemButton)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(closeButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
       if (predicateComboBox.getSelectedItem().getClass().equals(LogicPredicate.class)){
            JFrame editPredicateFrame = new EditPredicateBox((LogicPredicate<?>)predicateComboBox.getSelectedItem(),selectedFile);
            editPredicateFrame.setVisible(true);
       } else {
               if (predicateComboBox.getSelectedItem().getClass().equals(SkolemFunction.class)){

                   JFrame editSkolemFrame = new EditSkolemFunctionBox((SkolemFunction<String, String>)predicateComboBox.getSelectedItem(),selectedFile);
                   editSkolemFrame.setVisible(true);
                   
               } else {
                   ErrorFrame error = new ErrorFrame ("Illegal object in Database");
                   error.setVisible(true);
                   dispose();

               }
       }
               

    }//GEN-LAST:event_editButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        try {
            if (predicateComboBox.getSelectedItem().getClass().equals(SkolemFunction.class)){
                KnowledgeBaseManager.deleteSkolemFunction((SkolemFunction<String, String>) predicateComboBox.getSelectedItem(), selectedFile);
            }
            else{
                KnowledgeBaseManager.deletePredicateLogicElement((PredicateLogic) predicateComboBox.getSelectedItem(), selectedFile);
            }
            
        } catch (Exception ex) {

            ErrorFrame error = new ErrorFrame("Could not delete object from database. Please press refresh and try again");
            error.setVisible(true);
            ex.printStackTrace();


        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void newPredicateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newPredicateButtonActionPerformed
        JFrame newPredicateFrame = new NewLogicPredicateFrame(selectedFile);
        newPredicateFrame.setVisible(true);
    }//GEN-LAST:event_newPredicateButtonActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
       
       refresh();
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void newSkolemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newSkolemButtonActionPerformed
        JFrame newSkolem = new NewSkolemFunctionFrame(selectedFile);
        newSkolem.setVisible(true);
        
    }//GEN-LAST:event_newSkolemButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewKnowledgeBaseFrame().setVisible(true);
            }
        });
    }
    private void refresh(){
         try{
        elementList = null;
        elementList =KnowledgeBaseManager.ObtainEntireKnowledgeBase(selectedFile);
        predicateComboBox.setModel(new DefaultComboBoxModel(elementList.toArray()));
        } catch (Exception e ){
            e.printStackTrace();
            ErrorFrame error = new ErrorFrame("Database Error:"+e.getMessage());
            error.setVisible(true);
            this.dispose();

        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton editButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton newPredicateButton;
    private javax.swing.JButton newSkolemButton;
    private javax.swing.JComboBox predicateComboBox;
    private javax.swing.JButton refreshButton;
    // End of variables declaration//GEN-END:variables

}
